package com.ygkj.demo

import android.Manifest
import android.os.Bundle
import android.os.Environment
import android.text.TextUtils
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.RelativeLayout
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.ygkj.demo.viewholder.VideoHolder
import com.youth.banner.Banner
import com.youth.banner.listener.OnPageChangeListener
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import kotlin.system.exitProcess

class MainActivity : AppCompatActivity() {
    private var mExitTime: Long = 0
//    var player: StandardGSYVideoPlayer? = null

    var player: VideoView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        /*   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
               // 先判断有没有权限
               if (Environment.isExternalStorageManager().not()) {
                   val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
                   intent.data = Uri.parse("package:" + this.packageName)
                   startActivityForResult(intent, 1010)
               }
           }*/
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ),
            1001
        )
        bannerDemo()

    }

    private fun bannerDemo() {
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        layoutParams.width = 800
        layoutParams.height = 300
        layoutParams.leftMargin = 100
        layoutParams.topMargin = 100

        var banner = Banner<DataBean, MultipleTypesAdapter>(this)
        banner.layoutParams = layoutParams
        rootView.addView(banner)
        var multipleTypesAdapter = MultipleTypesAdapter(this, DataBean.getTestDataVideo())
        banner.addBannerLifecycleObserver(this)
            .setAdapter(multipleTypesAdapter)
            .isAutoLoop(true)
            .setUserInputEnabled(false)
            .setLoopTime(5_000)
    banner.viewPager2.offscreenPageLimit=5

        banner.addOnPageChangeListener(object :OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                var get1 = multipleTypesAdapter.vhMap

                Log.d("视屏","$position ==== ${ get1.size()}")


        }

        })
        multipleTypesAdapter.setOnBannerListener { data, position ->
            Log.d("视屏","$position")

        }
        multipleTypesAdapter.setOnScrollPageListener {
            var get1 = multipleTypesAdapter.vhMap
            Log.d("视屏自定义","$it ==== ${ get1.size()}")
            var get = multipleTypesAdapter.vhMap.get(it) as VideoHolder
            get.player.setUp(Environment.getExternalStoragePublicDirectory("MIPS2").getPath()+ File.separator +"d4d443b1f5094693a45530d72bf11338/upload/video/640a6f54eb2a4ad1b694a3b67029c862.mp4","")
            get.player.startButton.visibility=View.GONE
            get.player.startVideo()
        }
//        banner.currentItem=0

    }

    fun click(view: View) {
        var outpath1 = Environment.getExternalStoragePublicDirectory("MIPS2").path
        var outpath = ZipUtils.createFile(
            Environment.getExternalStoragePublicDirectory("MIPS2").path,
            "053cc53e996a44d3962cfab6edf554b8"
        ).path
        var zippath = "$outpath1${File.separator}053cc53e996a44d3962cfab6edf554b8.zip"
        ZipUtils.unzip(zippath, outpath)
    }

    //    override fun onPause() {
//        super.onPause()
//        if (player != null) player!!.onVideoPause()
//    }
//
//    override fun onResume() {
//        super.onResume()
//        if (player != null) player!!.onVideoResume()
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        GSYVideoManager.releaseAllVideos()
//    }
//
//    override fun onBackPressed() {
//        //释放所有
//        if (player != null) player!!.setVideoAllCallBack(null)
//        super.onBackPressed()
//    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 判断两次点击的时间间隔（默认设置为2秒）
            if (System.currentTimeMillis() - mExitTime < 2000) {
                toast("再按一次退出程序")
                mExitTime = System.currentTimeMillis()
            } else {
                this.finish()
                exitProcess(0)
            }
            return true

        }
        return super.onKeyDown(keyCode, event)
    }

}